﻿using UniRx;
using System;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;

namespace MGFramework
{
    public class PanelBase : MonoBehaviour
    {
        public delegate void Callback();
        private bool isClick = true;
        protected CanvasGroup cg;
        protected bool isInit;
        void Start()
        {
            cg = transform.GetComponent<CanvasGroup>();
            if (!isInit)
            {
                RegisterPanel();
                isInit = true;
                InitPanel();
            }
        }

        public void UICenterInitPanel()
        {
            cg = transform.GetComponent<CanvasGroup>();
            if (!isInit)
            {
                RegisterPanel();
                isInit = true;
                InitPanel();
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        protected virtual void InitPanel() { }

        /// <summary>
        /// 调用方法
        /// </summary>
        /// <param name="method">方法id</param>
        /// <param name="data">参数</param>
        public virtual void CallPanelMethod(int method, params object[] data) { }

        /// <summary>
        /// 注册到UICenter
        /// </summary>
        private void RegisterPanel()
        {
            UICenter.instance.RegisterPanel(this.name, this);
        }


        /// <summary>
        /// 绑定按键
        /// </summary>

        /// <param name="parent">父物体</param>
        /// <param name="path">以父物体为起点，按键的路径</param>
        /// <param name="cb">按键回调,如果有回调时，请在回调的逻辑运行完成后调用OpenClick()方法（按键切换界面时除外），否则按键将失灵</param>
        /// <param name="isAnim">是否播放默认的动画</param>
        /// <param name="autoOpenClick">有回调时，是否自动调用OpenClick()方法（默认为false，适用于不使用dotween动画时）</param>
        /// <returns></returns>
        protected Button BindButton(Transform parent, string path, Callback cb, bool isAnim = true, bool autoOpenClick = false)
        {
            Button temp_btn = null;
            if (path == null) temp_btn = parent.GetComponent<Button>();
            else temp_btn = parent.Find(path).GetComponent<Button>();
            try
            {
                if (isAnim)
                {
                    Vector3 orgin = new Vector3(temp_btn.transform.localScale.x, temp_btn.transform.localScale.y, temp_btn.transform.localScale.z);
                    Vector3 change = orgin * 1.2f;
                    temp_btn.OnClickAsObservable().Where(_ => isClick).Subscribe(_ =>
                    {
                        isClick = false;
                        temp_btn.transform.DOScale(change, 0.05f).OnComplete(() =>
                        {
                            temp_btn.transform.DOScale(orgin, 0.05f).OnComplete(() =>
                            {
                                if (cb != null)
                                {
                                    cb();
                                    if (autoOpenClick)
                                    {
                                        isClick = true;
                                    }
                                }
                                else
                                {
                                    isClick = true;
                                }
                            });
                        });
                    });
                }
                else
                {
                    temp_btn.OnClickAsObservable().Where(_ => isClick).Subscribe(_ =>
                    {
                        isClick = false;
                        if (cb != null) cb();
                        isClick = true;
                    });
                }
            }
            catch (Exception e)
            {
                Debug.LogError("绑定按键失败!");
                Debug.LogError(e);
            }
            return temp_btn;
        }

        /// <summary>
        /// 绑定按键
        /// </summary>
        /// <param name="parent">父物体</param>
        /// <param name="path">以父物体为起点，按键的路径</param>
        /// <returns></returns>
        protected Image BindImage(Transform parent, string path)
        {
            try
            {
                if (path == null) return parent.GetComponent<Image>();
                else return parent.Find(path).GetComponent<Image>();
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 绑定文本
        /// </summary>
        /// <param name="parent">父物体</param>
        /// <param name="path">以父物体为起点，按键的路径</param>
        /// <param name="content">默认内容</param>
        /// <returns></returns>
        protected Text BindText(Transform parent, string path, string content = null)
        {
            try
            {
                Text temp_text;
                if (path == null) temp_text = parent.GetComponent<Text>();
                else temp_text = parent.Find(path).GetComponent<Text>();
                if (content != null) temp_text.text = content;
                return temp_text;
            }
            catch
            {
                Debug.LogError("Bind text err!!!");
                return null;
            }
        }

        /// <summary>
        /// 关闭按键点击
        /// </summary>
        public void CloseClick()
        {
            isClick = false;
        }

        /// <summary>
        /// 打开按键点击
        /// </summary>
        public void OpenClick()
        {
            isClick = true;
        }

        /// <summary>
        /// 获取界面组组件
        /// </summary>
        /// <returns></returns>
        public CanvasGroup GetCanvasGroup()
        {
            return cg;
        }
    }
}
